package com.alibaba.citrus.ots.sales.plan.facade.service.setting;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.sales.plan.api.setting.SalesPlanStrategyQueryService;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.PageQuerySalesPlanStrategyRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.QuerySalesPlanStrategyRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.SalesOrgRatioDTO;
import com.alibaba.citrus.ots.sales.plan.repository.PlatformEnhanceRepository;
import com.alibaba.citrus.ots.sales.plan.repository.SalesPlanStrategyRepository;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanStrategySDO;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsplatformenhance.sdo.OrganizationSDO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class SalesPlanStrategyQueryServiceImpl implements SalesPlanStrategyQueryService {

    @Resource
    private SalesPlanStrategyRepository salesPlanStrategyRepository;

    @Resource
    private PlatformEnhanceRepository platformEnhanceRepository;

    @FacadeInvoker
    @Override
    public Result<PageResult<List<SalesPlanStrategySDO>>> pageQuerySalesPlanStrategyList(PageQuerySalesPlanStrategyRequest pageQuerySalesPlanStrategyRequest) {
        PageResult<List<SalesPlanStrategySDO>> listPageResult = salesPlanStrategyRepository.pageQuery(pageQuerySalesPlanStrategyRequest);
        return Result.success(listPageResult);
    }

    @FacadeInvoker
    @Override
    public Result<SalesPlanStrategySDO> queryProductPlanInfoById(QuerySalesPlanStrategyRequest querySalesPlanStrategyRequest) {
        SalesPlanStrategySDO salesPlanStrategySDO = salesPlanStrategyRepository.querySalesPlanStrategy(querySalesPlanStrategyRequest);
        refreshSalesPlanStrategy(salesPlanStrategySDO);
        return Result.success(salesPlanStrategySDO);
    }

    private void refreshSalesPlanStrategy(SalesPlanStrategySDO salesPlanStrategySDO) {
        if (null == salesPlanStrategySDO) {
            return;
        }
        List<SalesOrgRatioDTO> orgRatioDTOList = JSONArray.parseArray(salesPlanStrategySDO.getFeatures(), SalesOrgRatioDTO.class);
        if (CollectionUtils.isEmpty(orgRatioDTOList)) {
            return;
        }
        List<OrganizationSDO> organizationSDOList = platformEnhanceRepository.queryOrganizationByIds(orgRatioDTOList.stream().map(SalesOrgRatioDTO::getSalesOrgCode).collect(Collectors.toList()));
        if (CollectionUtils.isEmpty(organizationSDOList)) {
            return;
        }
        Map<String, OrganizationSDO> map = organizationSDOList.stream().collect(Collectors.toMap(OrganizationSDO::getId, Function.identity()));
        orgRatioDTOList.forEach(salesOrgRatioDTO -> {
            OrganizationSDO organizationSDO = map.get(salesOrgRatioDTO.getSalesOrgCode());
            salesOrgRatioDTO.setSalesOrgName(organizationSDO.getName());
        });
        salesPlanStrategySDO.setFeatures(JSONObject.toJSONString(orgRatioDTOList));
    }
}
